home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / sphigs / sph_mac.hqx / SRGP port to 5.0 (compressed) / SRGP_SPHIGS Root / MacSPHIGS / sph_cull.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-13  |  2.4 KB  |  85 lines

  1. #include "HEADERS.h"
  2. /**********************
  3.                       *
  4.  File                 * cull.c
  5.                       * backface culling unit
  6.                       *
  7.                       * changes p_normals in objects
  8.                       *
  9.                       *********************************************************/
  10.  
  11. /**********************
  12.                       *
  13.  Includes          *
  14.                       *
  15.                       *********************************************************/
  16.  
  17. #include <stdio.h>
  18. #include <assert.h>
  19.  
  20. #include "sphigslocal.h"
  21.  
  22. #include "sph_cull.proto.h"
  23.  
  24. /**********************
  25.                       *
  26.  Function          * cull
  27.                       *
  28.                       * reduces the number of nodes in the objects list
  29.                       * through backface culling with the perspectivized
  30.                       * normals
  31.                       *
  32.                       *********************************************************/
  33.  
  34. void SPH__cull (view_spec *vs)
  35. {
  36.      obj *            current;    /* current object */
  37.      obj *            prev;        /* keep the previous for node removal */
  38.      MAT3hvec            vec1;        /* subtracted vector 1 */
  39.      MAT3hvec            vec2;        /* subtracted vector 2 */
  40.      MAT3hvec            canon_normal;    /* canonized normal to the face */
  41.      double            length;        /* used in normalize */
  42.      
  43.      assert( vs != NULL );
  44.      
  45.      if( vs->objects == NULL )
  46.       return;
  47.      
  48.      prev = NULL;
  49.      current = vs->objects;
  50.      
  51.      prev = NULL;
  52.      while( current != NULL ) {
  53.       
  54.       if (current->type == objFace) {
  55.            
  56.            /* Calculate normal */
  57.            MAT3_SUB_VEC (vec1,
  58.                  vs->npcVertices[current->data.face.points[1]],
  59.                  vs->npcVertices[current->data.face.points[0]]);
  60.            MAT3_SUB_VEC (vec2,
  61.                  vs->npcVertices[current->data.face.points[2]],
  62.                  vs->npcVertices[current->data.face.points[1]]);
  63.            vec1[3] = 1;
  64.            vec2[3] = 1;
  65.            MAT3_NORMALIZE_VEC( vec1, length );
  66.            MAT3_NORMALIZE_VEC( vec2, length );
  67.            MAT3cross_product (canon_normal, vec1, vec2);
  68.            canon_normal[3] = 1;
  69.            MAT3_NORMALIZE_VEC( canon_normal, length );
  70.            MAT3_COPY_VEC( current->p_normal, canon_normal );
  71.            current->p_normal[3] = 1;
  72.            
  73.            if( current->p_normal[Z] <= 0 ) {
  74.             if( prev == NULL )
  75.              vs->objects = current->next;
  76.             else
  77.              prev->next = current->next;
  78.            } else
  79.             prev = current;
  80.            
  81.            current = current -> next;
  82.       }
  83.      }
  84. }
  85.